<?php

namespace App\Http\Controllers\Admin\System;

use App\Http\Controllers\Controller;
use App\Models\SystemConfig;
use App\Traits\Controllers\DestroyTraits;
use App\Traits\Controllers\ShowTraits;
use App\Transformers\System\SystemConfigTransformer;
use Illuminate\Http\Request;

class SystemConfigController extends Controller
{
    use ShowTraits,DestroyTraits;

    public  $model = SystemConfig::class ;
    public  $transformer = SystemConfigTransformer::class ;

    /**
     * 配置详情
     */
    public function index($id)
    {
        $systemConfig = SystemConfig::where('category_id',$id)->get();

        return $this->response->collection($systemConfig, new SystemConfigTransformer);
    }

    /**
     * 添加配置字段z
     */
    public function store(Request $request)
    {
        $type = $request->input('type');

        $this->check($request,$type);

        $request->offsetSet('system_admin_id', $this->admin_user('id'));

        $model = $this->getQuery()->fill($request->all());

        $model->save();

        return $this->response->created();
    }

    public function show(Request $request)
    {
        $show = $this->getQuery()->findOrFail($request->index);// findOrFail 查找失败时抛出异常404

        if ($this->policy) {
            $this->authorize('show', $show);
            // authorize 控制器授权  当你尝试授权一个动作并且在用户不允许执行给定动作时抛出授权异常
            //那么使用authorize()方法，会在异常时返回一个异常403响应错误。
        }


        return $this->response->item($show, resolve($this->transformer));
    }

    /**
     *  更新配置字段
     */
    public function update(Request $request,SystemConfig $config)
    {
        $type = $request->input('type') ? $request->input('type') : $config->type;

        $this->check($request,$type);

        $config = $config->find($request->index);

        $config->fill($request->all());

        $config->save();

        return $this->response->noContent();
    }

    public function destroy(Request $request)
    {
        //explode() 函数把字符串打散为数组。
        //array_filter() 函数用回调函数过滤数组中的元素。删除数组中 array 的所有“空”元素
        $ids = array_filter(explode(',', $request->index));

        if (empty($ids)) {
            return $this->response->errorBadRequest();
        }
        $this->getQuery()->whereIn($this->getQuery()->getKeyName(), $ids)->delete();

        return $this->response->noContent();
    }

    /**
     * 排序
     */
    public function sort(Request $request, SystemConfig $config)
    {
        $config = $config->find($request->index);

        $config->sort = $request->input('sort',0);
        $config->save();
        return $this->response->noContent();
    }

    /**
     * 状态禁用和启用
     */
    public function status(SystemConfig $config)
    {
        $config->status = $config->status == 0 ? 1 : 0;
        $config->save();

        return $this->response->noContent();
    }

    public function check(Request $request, $type){

        $request->validate([
            'menu_name' => [
                'required',
                'min:2',
                'max:30'
            ],
            'type' => [
                'required',
                'in:text,Uploads,radio,textarea'
            ],
            'info' => [
                'required',
            ]
        ],[
            'menu_name.required' => '配置英文名称 不能为空',
            'menu_name.min' => '配置英文名称 不能小于2',
            'menu_name.max' => '配置英文名称 不能大于30',
            'type.required' => '类型 不能为空',
            'type.in' => '类型 不许在 text,Uploads,radio,textarea 中',
            'info.required' => '配置中文名称 不能为空',
        ]);

        if ($type == 'text'){

            $request->validate([
                'width' => [
                    'required',
                    'numeric'
                ]
            ],[
                'width.required' => '宽度 不能为空',
                'numeric' => '宽度 必须为数字'
            ]);
        }

        if ($type == 'textarea'){

            $request->validate([
                'width' => [
                    'required',
                    'numeric',
                ],
                'height' => [
                    'required',
                    'numeric',
                ],
            ],[
                'width.required' => '宽度 不能为空',
                'numeric' => '宽度 必须为数字',
                'height.required' => '高度 不能为空',
                'height.size' => '高度 大小为1',
            ]);
        }

        if ($type == 'Uploads'){

            $request->validate([
                'upload_type' => [
                    'required',
                    'in:image,images,file',
                ],
            ],[
                'upload_type.required' => '上传类型 不能为空',
                'in' => '上传类型 必须为image,images,file',
            ]);
        }

        if ($type == 'radio'){

            $request->validate([
                'parameter' => [
                    'required',
                ],
            ],[
                'parameter.required' => '参数 不能为空',
            ]);
        }
    }

}
